package com.zyf.spit.controller;

import com.zyf.spit.pojo.Spit;
import com.zyf.spit.service.impl.SpitServiceImpl;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class SpitController {
    @Autowired
    SpitServiceImpl spitService;

    @Autowired
    RedisTemplate redisTemplate;
    /*
    根据userid获得content分页列表
     */
    @RequestMapping(value = "/selectContentListByUserId", method = RequestMethod.GET)
    public Result selectContentListByUserId(@RequestParam String userid,
                                            @RequestParam(defaultValue = "1") int pageNum,
                                            @RequestParam(defaultValue = "5") int pageSize) {
        List list = new ArrayList();
        list.add(spitService.selectContentListByUserId(userid, pageNum, pageSize));
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /*点赞
    根据id给thumpub加一
     */
    @RequestMapping(value = "/addThumbupById", method = RequestMethod.PUT)
    public Result addThumbupById(String id) {

        if (redisTemplate.opsForValue().get("thumbup_"+id)==null){
            spitService.deleteTumbupById(id);
            redisTemplate.delete("thumbup_"+id);
            return new Result(true, StatusCode.OK, "取消点赞成功");
        }
        spitService.addThumbupById(id);
        redisTemplate.opsForValue().set("thumbup_"+id,1);
        return new Result(true, StatusCode.OK, "点赞成功");

    }
    /*
    发布吐槽
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(Spit spit){
        spitService.add(spit);
        return new Result(true, StatusCode.OK, "吐槽成功");
    }
    /*
    增加浏览量与分享数
     */
    @RequestMapping(value = "/")
    public Result addVisitsAndShare(String id,Integer visits,Integer share){
        spitService.addVisitsAndShare(id,visits,share);
        return new Result(true, StatusCode.OK, "增加成功");
    }
}
